﻿using AutoStar.Common;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AutoStar.ViewModel
{
    public class PrintLatyerVM : BaseViewModel
    {
        public PrintLatyerVM()
        {
            OKCommand = new RelayCommand(OKCommand_Execute);
            ComboBox_SelectionChanged = new RelayCommand(ComboBox_SelectionChanged_Handler);
            SelectedPrintWay = true;
        }

        private void ComboBox_SelectionChanged_Handler()
        {
            VertexX = Global.printProject.printChannels[SelectedChannel1].position.X.ToString();
            VertexY = Global.printProject.printChannels[SelectedChannel1].position.Y.ToString();
            VertexZ = Global.printProject.printChannels[SelectedChannel1].position.Z.ToString();
        }

        private void OKCommand_Execute()
        {
            //保存所有text 提交到哪里? 保存配置?还是仅限于本次运行
            if (Global.printLayer.mode >= 10)//如果是通过分层切片方式自动添加的打印层
            {
                //修改层高
                for (int i = 0; i < Global.printLayer.points.Count; i++)
                {
                    Global.printLayer.points[i].Z = float.Parse(Global.layerVM.X);
                }
                Global.printLayer.z_start = float.Parse(Global.layerVM.Z);
                //修改固化时间
                Global.printLayer.solidfyTime = int.Parse(Global.layerVM.Time);
                //修改通道
                if (Global.layerVM.SelectedChannel == "通道1")
                    Global.printLayer.channel = 0;
                else if (Global.layerVM.SelectedChannel == "通道2")
                    Global.printLayer.channel = 1;
                else if (Global.layerVM.SelectedChannel == "通道3")
                    Global.printLayer.channel = 2;
                else if (Global.layerVM.SelectedChannel == "通道4")
                    Global.printLayer.channel = 3;
                else if (Global.layerVM.SelectedChannel == "通道5")
                    Global.printLayer.channel = 4;
                else if (Global.layerVM.SelectedChannel == "通道6")
                    Global.printLayer.channel = 5;
                else if (Global.layerVM.SelectedChannel == "通道7")
                    Global.printLayer.channel = 6;
                AddLayerEvent(Global.printLayer);
            }
            else
            {
                Global.printProject.printLayers.Add(new PrintLayer(Global.layerVM.Tag, Global.printLayer.channel,
                    float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width),
                    float.Parse(Global.layerVM.Length), float.Parse(Global.layerVM.X),
                    float.Parse(Global.layerVM.Y), float.Parse(Global.layerVM.Z),
                    int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));
                Global.MainVM.flushDgv();
                //Global.SliceVM.EditLayer_AddLayerEvent(new PrintLayer(Global.layerVM.Tag, Global.printLayer.channel,
                //    float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width),
                //    float.Parse(Global.layerVM.Length), float.Parse(Global.layerVM.X),
                //    float.Parse(Global.layerVM.Y), float.Parse(Global.layerVM.Z),
                //    int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));
                //AddLayerEvent(new PrintLayer(Global.layerVM.Tag, Global.printLayer.channel, 
                //    float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width), 
                //    float.Parse(Global.layerVM.Length), float.Parse(Global.layerVM.X), 
                //    float.Parse(Global.layerVM.Y), float.Parse(Global.layerVM.Z),
                //    int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));
            }
        }
        //声明委托
        public delegate void LayerDelegate(PrintLayer layerChanged);
        //声明事件
        public event LayerDelegate AddLayerEvent;
        public ICommand OKCommand { get; set; }
        public ICommand CancelCommand { get; set; }
        public ICommand ComboBox_SelectionChanged { get; private set; }
        private int _SelectedChannel1 = 0;
        public int SelectedChannel1
        {
            get => _SelectedChannel1;
            set => Set(ref _SelectedChannel1, value);
        }
        private bool _SelectedPrintWay;
        public bool SelectedPrintWay
        {
            get => _SelectedPrintWay;
            set
            {
                Set(ref _SelectedPrintWay, value);
                if (value)
                {
                    SelectedPrintWay1 = false;
                    SelectedPrintWay2 = false;
                }
            }
        }
        private bool _SelectedPrintWay1;
        public bool SelectedPrintWay1
        {
            get => _SelectedPrintWay1;
            set
            {
                Set(ref _SelectedPrintWay1, value);
                if (value)
                {
                    SelectedPrintWay = false;
                    SelectedPrintWay2 = false;
                }
            }
        }
        private bool _SelectedPrintWay2;
        public bool SelectedPrintWay2
        {
            get => _SelectedPrintWay2;
            set
            {
                Set(ref _SelectedPrintWay2, value);
                if (value)
                {
                    SelectedPrintWay = false;
                    SelectedPrintWay1 = false;
                }
            }
        }
        private string _VertexX;
        public string VertexX
        {
            get => _VertexX = Global.printStatusVM.lblX;
            set => Set(ref _VertexX, value);
        }
        private string _VertexY;
        public string VertexY
        {
            get => _VertexY = Global.printStatusVM.lblY;
            set => Set(ref _VertexY, value);
        }
        private string _VertexZ;
        public string VertexZ
        {
            get => _VertexZ = Global.printStatusVM.lblZ;
            set => Set(ref _VertexZ, value);
        }
        private string _Width;
        public string Width
        {
            get => _Width;
            set => Set(ref _Width, value);
        }
        private string _Length;
        public string Length
        {
            get => _Length;
            set => Set(ref _Length, value);
        }
        private string _Interval;
        public string Interval
        {
            get => _Interval;
            set => Set(ref _Interval, value);
        }
        private string _Height;
        public string Height
        {
            get => _Height;
            set => Set(ref _Height, value);
        }
        private string _Time;
        public string Time
        {
            get => _Time;
            set => Set(ref _Time, value);
        }
    }
}
